package com.ptit.giaothong.xebus;

import java.util.ArrayList;
import java.util.List;

import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ptit.giaothong.MainActivity;
import com.ptit.giaothong.R;

public class XebusActivity extends Activity {
	private ListView listview;
	private List<Itembus> list;
	private Activity activity;
	public static int pagageIndex = 0;
	private List<Itembus> listSmall;
	private ItemAdapter itemAdapter = null;

	public XebusActivity(Activity activity) {
		this.activity = activity;
	}

	public void init() {
		activity.setContentView(R.layout.busactivity);
		MainActivity.keypage = 1;

		String info[] = activity.getResources().getStringArray(
				R.array.lotrinh_bus);
		list = new ArrayList<Itembus>();

		for (int i = 0; i < info.length; i++) {
			Itembus item = new Itembus();
			String iteminfo[] = info[i].split("\\|");
			item.setName(iteminfo[0]);
			item.setTansuat(iteminfo[1]);
			item.setTime(iteminfo[2]);
			item.setPrice(iteminfo[3]);
			item.setQuangduong(iteminfo[4]);
			item.setTotaltime(iteminfo[5]);
			item.setLuotdi(iteminfo[6]);
			item.setLuotve(iteminfo[7]);
			list.add(item);
		}

		listview = (ListView) activity.findViewById(R.id.listViewbb);
		itemAdapter = new ItemAdapter(activity, R.layout.itembus, list);
		listview.setAdapter(itemAdapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int postion, long arg3) {
				Busdetailt busdetailt = new Busdetailt(activity);
				busdetailt.Init(list.get(postion));

			}
		});

		final EditText txtdiemxp = (EditText) activity
				.findViewById(R.id.txtdiemxuatphat);
		txtdiemxp.addTextChangedListener(new TextWatcher() {
			final List<Itembus> listSearch = new ArrayList<Itembus>();

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

				String searchString = txtdiemxp.getText().toString().trim()
						.toLowerCase();
				if (searchString.length() > 0) {
					listSearch.clear();

					for (Itembus itemSearch : list) {
						boolean isSearched = false;
						String nameXp[] = itemSearch.getLuotdi().split("\\-");
						for (int j = 0; j < nameXp.length; j++) {
							if (nameXp[j].toLowerCase().contains(searchString)) {
								isSearched = true;
							
							}

						}
						if (isSearched) {
							listSearch.add(itemSearch);
						}
					}

					if (listSearch.size() > 0) {
						itemAdapter = new ItemAdapter(activity,
								R.layout.itembus, listSearch);
					} else {
						// tView.setVisibility(View.VISIBLE);
					}
				} else {
					// tView.setVisibility(View.GONE);
					itemAdapter = new ItemAdapter(activity, R.layout.itembus,
							list);

				}
				listview.setAdapter(itemAdapter);

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		TextView txtdiemden = (TextView) activity.findViewById(R.id.txtdiemdem);
		txtdiemden.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

	}

	static class ItemAdapter extends ArrayAdapter<Itembus> {
		private int resource;
		private LayoutInflater layoutInflater;

		public ItemAdapter(Context mContext, int resource, List<Itembus> models) {
			super(mContext, resource, models);
			this.resource = resource;
			layoutInflater = LayoutInflater.from(mContext);
		}

		public class ViewHolder {
			public TextView lottrinh;
			public TextView name;
			public ImageView thumnail;

		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = layoutInflater.inflate(resource, null, true);
				viewHolder.name = (TextView) convertView
						.findViewById(R.id.txttuyen);
				viewHolder.lottrinh = (TextView) convertView
						.findViewById(R.id.txtlotrinh);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			Itembus item = getItem(position);
			viewHolder.name.setText(item.getName());
			viewHolder.lottrinh
					.setText("Thời gian hoạt động:" + item.getTime());
			return convertView;
		}
	}

	@Override
	public void onBackPressed() {
		switch (pagageIndex) {
		case 0:
			MainActivity.getdialogMesg(activity, "Bạn có thực sự muốn thoát");
			break;
		case 1:
			init();
			break;

		}
	}
}
